<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
include_once("model/BgcmsUploadBean.class.php");
class BgcmsUpload 
{

 	
    function BgcmsUpload()
	{
		//store pointer to db onject

	}
	
      
      function crop_image($nw, $nh, $source, $stype, $dest) {
          $size = getimagesize($source);
          $w = $size[0];
          $h = $size[1];
          switch($stype) {
              case 'gif':
              $simg = imagecreatefromgif($source);
              break;
              case 'jpg':
              $simg = imagecreatefromjpeg($source);
              break;
              case 'png':
              $simg = imagecreatefrompng($source);
              break;
          }
          $dimg = imagecreatetruecolor($nw, $nh);
          $wm = $w/$nw;
          $hm = $h/$nh;
          $h_height = $nh/2;
          $w_height = $nw/2;
		  $src_x = $w - $nw;
		  $dst_h = $h - $nh;
          if($w> $h) {
				if($w/$h> $nw/$nh) {
					$adjusted_width = $w / $hm;
					$half_width = $adjusted_width / 2;
					$int_width = $half_width - $w_height;
					imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
				}else{
					$adjusted_height = $h / $wm;
					$half_height = $adjusted_height / 2;
					$int_height = $half_height - $h_height;
					imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
				}
			} elseif(($w <$h) || ($w == $h)) {
				$adjusted_height = $h / $wm;
			  
				$half_height = $adjusted_height / 2;
				$int_height = $half_height - $h_height;
				imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
			} else {
				imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
			}
          imagejpeg($dimg,$dest,100);
      }

	
	function upload_banner_image($image, $template, $type) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
		} else if($type == "bmp") {
			$src = BgcmsUploadBean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($width >= $height) {
			if($width >= 880) {
				$newwidth = 880;
				$newheight =($height/$width)*880;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		} else {
			if($width >= 318) {
				$newwidth = 318;
				$newheight =($height/$width)*318;
			} else {
				$newwidth = $width;
				$newheight = $height;
			}
		}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
				
		$$name = $template."_".date("YmdHms").".$type";

		$target = "../".UPLOAD.$template."_".date("YmdHms").".$type";
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			imagepng($tmp, $target);
		} else if($type == "bmp") {
			BgcmsUploadBean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		//BgcmsUpload::crop_image(120, 150, $target, $type, $target1);	
		
		return $$name;
	}
	
	
	
	
	function upload_files($image, $photo, $type) {
		$error = -1;
	   if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$source = $image;
			if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
				$src = imagecreatefromjpeg($source);
			} else if($type == "gif") {
				$src = imagecreatefromgif($source);
			} else if($type == "png") {
				$src = imagecreatefrompng($source);
			} else if($type == "bmp") {
				$src = BgcmsUploadBean::imagecreatefrombmp($source);
			}
			list($width,$height) = getimagesize($source);
			if($width >= $height) {
				if($width >= 666) {
					$newwidth = 666;
					$newheight =($height/$width)*666;
				} else {
					$newwidth = $width;
					$newheight = $height;
				}
			} else {
				if($width >= 318) {
					$newwidth = 318;
					$newheight =($height/$width)*318;
				} else {
					$newwidth = $width;
					$newheight = $height;
				}
			}
			$tmp=imagecreatetruecolor($newwidth,$newheight);
			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
					
			$$name = $photo."_".date("YmdHms").".$type";
	
			$target = "../".PATH_FILES.$photo."_".date("YmdHms").".$type";
			
			if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
				imagejpeg($tmp, $target);
			} else if($type == "gif") {
				imagegif($tmp, $target);
			} else if($type == "png") {
				imagepng($tmp, $target);
			} else if($type == "bmp") {
				BgcmsUploadBean::imagebmp($tmp, $target);
			}
						
			imagedestroy($src);
			imagedestroy($tmp);
			
			return $$name;
		}else{
		   if (is_uploaded_file($image)) {
				 $randomname = $photo.date("YmdHms").mt_rand();		
				 $$name = md5($randomname).".$type";
				 $result = move_uploaded_file($image, "../".PATH_FILES.$$name);
				 if ($result == 1) echo "<p>File successfully uploaded.</p>";
				 else echo "<p>There was a problem uploading the file.</p>";
		   } 
		}
	}
	
	function upload_flag($image, $photo, $type) {
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			$src = imagecreatefromjpeg($source);
		} else if($type == "gif") {
			$src = imagecreatefromgif($source);
		} else if($type == "png") {
			$src = imagecreatefrompng($source);
				imagealphablending($src, false);
				imagesavealpha($src, true);
		} else if($type == "bmp") {
			$src = BgcmsUploadBean::imagecreatefrombmp($source);
		}
		$$name = $photo."_".date("YmdHms").".$type";
		$target = "../".UPLOAD_FLAG.$photo."_".date("YmdHms").".$type";
		
		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {
			imagejpeg($tmp, $target);
		} else if($type == "gif") {
			imagegif($tmp, $target);
		} else if($type == "png") {
			header('Content-Type: image/png');
			imagepng($src, $target);
		} else if($type == "bmp") {
			BgcmsUploadBean::imagebmp($tmp, $target);
		}
					
		imagedestroy($src);
		imagedestroy($tmp);
		
		return $$name;
	}
	
	public function upload_news_thumbnail($image, $userid, $type,$small,$large) {

  		$error = -1;

  		

  		$source = $image;

  		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {

  			$src = imagecreatefromjpeg($source);

  		} else if($type == "gif") {

  			$src = imagecreatefromgif($source);

  		} else if($type == "png") {

  			$src = imagecreatefrompng($source);

  		} else if($type == "bmp") {

  			$src = UploadBean::imagecreatefrombmp($source);

  		}

  		list($width,$height) = getimagesize($source);

  		if($width >= $height) {

  			if($width >= 666) {

  				$newwidth = 666;

  				$newheight =($height/$width)*666;

  			} else {

  				$newwidth = $width;

  				$newheight = $height;

  			}

  		} else {

  			if($width >= 318) {

  				$newwidth = 318;

  				$newheight =($height/$width)*318;

  			} else {

  				$newwidth = $width;

  				$newheight = $height;

  			}

  		}

  		$tmp=imagecreatetruecolor($newwidth,$newheight);

  		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

  				
		$randomname = $userid.date("YmdHms").mt_rand();
  		$$name = md5($randomname).".$type";

  

  		$target = $large.$$name;

  		$target1 = $small.$$name;

  		

  		if($type == "jpeg" || $type == "jpe" || $type == "jpg") {

  			imagejpeg($tmp, $target);

  		} else if($type == "gif") {

  			imagegif($tmp, $target);

  		} else if($type == "png") {

  			imagepng($tmp, $target);

  		} else if($type == "bmp") {

  			UploadBean::imagebmp($tmp, $target);

  		}

  					

  		imagedestroy($src);

  		imagedestroy($tmp);
  		$this->crop_image(120, 85, $target, $type, $target1);

  		return $$name;

  	}
	
}
?>